package com.szl.group.common.model;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * description: 分页封装.
 * author: shenxiaolong
 * date: 2020/6/20  16:54
 */
@Data
@Builder
@AllArgsConstructor
@ApiModel(value = "分页包装对象", description = "分页包装对象")
public class PageInfo<T> {

    public static final Long DEFAULT_PAGE_SIZE = 10L;
    public static final Long DEFAULT_PAGE_NUMBER = 1L;

    @ApiModelProperty(value = "pageSize", example = "1")
    private Long pageSize;
    @ApiModelProperty(value = "currentPage", example = "2")
    private Long currentPage;
    @ApiModelProperty(value = "total", example = "12")
    private Long total;

    @Builder.Default
    private List<T> records = Collections.emptyList();

    /**
     * 构造器.
     */
    public PageInfo(Page<T> page) {
        this.currentPage = page.getCurrent();
        this.pageSize = page.getSize();
        this.total = page.getTotal();
        this.records = page.getRecords();
    }


    public PageInfo() {

    }

    public PageInfo(Long currentPage, Long pageSize) {
        if (Objects.nonNull(currentPage)) {
            this.currentPage = currentPage;
        } else {
            this.currentPage = DEFAULT_PAGE_NUMBER;
        }
        if (Objects.nonNull(pageSize)) {
            this.pageSize = pageSize;
        } else {
            this.pageSize = DEFAULT_PAGE_SIZE;
        }
    }
}
